<?php
namespace app\common\controller;

use app\common\exception\ForbidenException;
use app\common\model\SystemConfig as SystemConfigModel;
use think\App;
use think\Controller;
use think\facade\Session;
use app\common\model\Auth as AuthModel;

class Base extends Controller
{
    //当前的模块
    protected $current_module = 'common';

    //当前的控制器
    protected $current_controller = '';

    //当前的方法
    protected $current_action = '';

    protected $breads = array();

    protected $is_show = 1;

    //排序 升序
    protected $sort_asc  = 'sort asc';

    //排序 降序
    protected $sort_desc = 'sort desc';

    //分页  每页显示的条数
    protected $per_page_array = array(
        array('per_page' => 20,  'per_page_label'  => '20条'),
        array('per_page' => 30,  'per_page_label'  => '30条'),
        array('per_page' => 40,  'per_page_label'  => '40条'),
        array('per_page' => 50,  'per_page_label'  => '50条'),
        array('per_page' => 60,  'per_page_label'  => '60条'),
        array('per_page' => 80,  'per_page_label'  => '80条'),
        array('per_page' => 100,  'per_page_label'  => '100条'),
        array('per_page' => 500,  'per_page_label'  => '500条'),
        array('per_page' => 1000,  'per_page_label'  => '1000条'),
    );

    public function __construct(App $app = null)
    {
        parent::__construct($app);

        $this->current_module = $this->request->module();
        $this->current_controller = $this->request->controller();
        $this->current_action = $this->request->action();


    }

    protected function getSystemConfig()
    {
        $configs = SystemConfigModel::order('config_sort asc')->select()->toArray();
        $configs = array_column($configs, 'config_value', 'config_key');

        return $configs;
    }


    /**
     * @param $total记录的总数
     * @param $per_page每页显示的条数
     * @param int $page当前第几页
     * @param string $url分页跳转的链接
     * @param array $params额外的参数
     * @return string
     */
    protected function page_html($total, $per_page, $page = 1, $url = '', $params = array())
    {
        if (!empty($params) || (!is_array($params))) {
            $params = (array)$params;
        }

        $per_page_array = $this->per_page_array;
        $total_page = ceil($total/$per_page);

        //分页的原理
        if ($page - 10/2  < 1) {
            $start = 1;
        } else {
            $start = $page - 10/2;
        }
        $end = 10 + $start;
        if ($end > $total_page) {
            $end = $total_page;
            $start = $total_page - 10;
            if ($start <= 0) {
                $start = 1;
            }
        }

        $page_html = '';
        $page_html .= '<div class="page">';
        $page_html .= '<div>';
        $page_html .= '<span style="margin-left:2px;margin-right:2px;">共'.$total.'条</span>';
        if ($page > 1) {
            $page_html .= '<a style="margin-left:2px;margin-right:2px;" class="prev" href="'. $url . '?page=' . ($page - 1) . '&per_page=' . $per_page . '&'.http_build_query($params, '', '&').'">&lt;&lt;</a>';
        }

        for ($i = $start; $i <= $end; $i++) {
            if ($i == $page) {
                $page_html .= '<span style="margin-left:2px;margin-right:2px;" class="current">'.$i.'</span>';
            } else {
                $page_html .= '<a style="margin-left:2px;margin-right:2px;" class="num" href="'. $url . '?page=' . ($i) . '&per_page=' . $per_page . '&'.http_build_query($params, '', '&').'">'.$i.'</a>';
            }
        }

        if ($page < $total_page) {
            $page_html .= '<a style="margin-left:2px;margin-right:2px;" class="next" href="'. $url . '?page=' . ($page + 1) . '&per_page=' . $per_page . '&'.http_build_query($params, '', '&').'">&gt;&gt;</a>';
        }

        $page_html .= '<div style="margin-left:2px;margin-right:2px;" class="layui-input-inline layui-show-xs-block">';
        $page_html .= '<form class="layui-form">';

        if (count($params)) {
            foreach ($params as $key => $param) {
                $page_html .= '<input type="hidden" name='.$key.' value='.$param.'>';
            }
        }

        $page_html .= '<div class="layui-input-inline" style="width:80px;">';
        $page_html .= '<select name="per_page" lay-filter="per_page">';
        foreach ($per_page_array as $per_page_value) {
            $temp_selected = '';
            if ($per_page == $per_page_value['per_page']) {
                $temp_selected = "selected='selected'";
            }
            $page_html .= '<option ' . $temp_selected . ' value="'.$per_page_value['per_page'].'">'.$per_page_value['per_page_label'].'</option>';
        }
        $page_html .= '</select>';

        $page_html .= '<div class="layui-unselect layui-form-select">';
        $page_html .= '<div class="layui-select-title">
                           <input type="text" placeholder="每页条数" value="0" readonly="" class="layui-input layui-unselect">
                           <i class="layui-edge"></i>
                      </div>';
        $page_html .= '<dl class="layui-anim layui-anim-upbit" style="">';
        foreach ($per_page_array as $per_page_value) {
            $temp_class = '';
            if ($per_page == $per_page_value['per_page']) {
                $temp_class = 'layui-this';
            }
            $page_html .= '<dd lay-value="'.$per_page_value['per_page'].'" class="'.$temp_class.'">'.$per_page_value['per_page'].'</dd>';
        }
        $page_html .= '</dl>';

        $page_html .= '</div>';
        $page_html .= '</div>';
        $page_html .= '</form>';
        $page_html .= '</div>';
        $page_html .= '</div>';
        $page_html .= '</div>';

        return $page_html;
    }


    /**
     * Name:newtree
     * @param $arr
     * @param int $pid
     * @param string $id_key
     * @param string $parent_id_key
     * @param string $children_name
     * @param bool $flag
     * @return array
     */
    protected function newtree($arr, $pid = 0, $id_key = 'id', $parent_id_key='parent_id', $children_name='children', $flag = false)
    {
        $method_name = __METHOD__;
        $method_name = explode('::', $method_name);
        $method_name = end($method_name);
        $temp = array();
        if ($flag) {
            $temp = array();
        }
        if (empty($arr)) {
            return $temp;
        }

        foreach ($arr as $key => $value) {
            if ($value[$parent_id_key] == $pid) {
                $value[$children_name] = $this->$method_name($arr, $value[$id_key], $id_key, $parent_id_key, $children_name);
                $temp[] = $value;
            }
        }

        return $temp;
    }

    protected function new_list_order($arr, $pid = 0, $id_key = 'id', $parent_id_key = 'parent_id', $flag = false)
    {
        $method_name = __METHOD__;
        $method_name = explode('::', $method_name);
        $method_name = end($method_name);

        static $temp = array();
        if ($flag) {
            $temp = array();
        }

        if (empty($arr)) {
            return $temp;
        }

        foreach ($arr as $value) {
            if ($value[$parent_id_key] == $pid) {
                if ($value['level'] == 1) {
                    $value['level_line'] = '';
                } else {
                    $value['level_line'] = '└─' . str_repeat('----', $value['level']);
                }
                $temp[] = $value;
                $this->$method_name($arr, $value[$id_key], $id_key, $parent_id_key, false);
            }
        }

        return $temp;
    }

    protected function getParents($arr, $current, $id_key = 'id', $parent_id_key = 'parent_id', $flag = false)
    {
        $method_name = __METHOD__;
        $method_name = explode('::', $method_name);
        $method_name = end($method_name);

        static $temp = array();
        if ($flag) {
            $temp = array();
        }
        if (empty($arr)) {
            return $temp;
        }

        foreach ($arr as $key => $value) {
            if ($value[$id_key] == $current[$parent_id_key]) {
                $temp[] = $value;
                $this->$method_name($arr, $value, $id_key, $parent_id_key, false);
            }
        }

        return $temp;
    }

    protected function getChildren($arr, $current, $id_key = 'id', $parent_id_key = 'parent_id', $flag=false)
    {
        $method_name = __METHOD__;
        $method_name = explode('::', $method_name);
        $method_name = end($method_name);

        static $temp = array();
        if ($flag) {
            $temp = array();
        }
        if (empty($arr)) {
            return $temp;
        }

        foreach ($arr as $key => $value) {
            if ($value[$parent_id_key] == $current[$id_key]) {
                $temp[] = $value;
                $this->$method_name($arr, $value, $id_key, $parent_id_key, false);
            }
        }

        return $temp;
    }



}